﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AdventureWorksWebApp
{
    public partial class details : System.Web.UI.Page
    {
        protected int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (int.TryParse(Request.QueryString["id"], out id))
            {
                if (!IsPostBack)
                {
                    LoadData();
                    EditOFF();
                }
            }
            else
            {
                //brak id w parametrze
            }
            Page.DataBind();
        }

        private void SaveData()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var produkt = (from prod in dc.Products
                               where prod.ProductID == id
                               select prod).Single();
                produkt.Name = txtProductName.Text;
                produkt.ProductNumber = txtProductNumber.Text;
                produkt.DaysToManufacture = int.Parse(txtDaysToManufacture.Text);
                produkt.ListPrice = decimal.Parse(txtListPrice.Text);
                produkt.ReorderPoint = short.Parse(txtReorderPoint.Text);
                produkt.rowguid = Guid.Parse(txtRowGUID.Text);
                produkt.SafetyStockLevel = short.Parse(txtSafetyStockLevel.Text);
                produkt.StandardCost = decimal.Parse(txtStandardCost.Text);
                produkt.FinishedGoodsFlag = chbFinishedGoodsFlag.Checked;
                produkt.MakeFlag = chbMakeFlag.Checked;

                produkt.ProductSubcategoryID = int.Parse(ddSubcategory.SelectedItem.Value);

                dc.SubmitChanges();
            }
            LoadData();
        }

        private void LoadData()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var produkt = (from prod in dc.Products
                               where prod.ProductID == id
                               select prod).Single();
                txtProductName.Text = produkt.Name;
                txtProductNumber.Text = produkt.ProductNumber;
                txtDaysToManufacture.Text = produkt.DaysToManufacture.ToString();
                txtListPrice.Text = produkt.ListPrice.ToString();
                txtReorderPoint.Text = produkt.ReorderPoint.ToString();
                txtRowGUID.Text = produkt.rowguid.ToString();
                txtSafetyStockLevel.Text = produkt.SafetyStockLevel.ToString();
                txtStandardCost.Text = produkt.StandardCost.ToString();
                chbFinishedGoodsFlag.Checked = produkt.FinishedGoodsFlag;
                chbMakeFlag.Checked = produkt.MakeFlag;
                
                     
                var x = from sub in dc.ProductSubcategories
                        where sub.ProductSubcategoryID == produkt.ProductSubcategoryID
                        select sub;
                if (x.Count() > 0)
                {
                    txtSubcategory.Text = x.Single().Name;
                    var y = from cat in dc.ProductCategories
                            where cat.ProductCategoryID == x.Single().ProductCategoryID
                            select cat;
                    if (y.Count() > 0)
                    {
                        txtCategory.Text = y.Single().Name;
                    }
                    else
                        txtCategory.Text = "No category";
                }
                else
                {
                    txtSubcategory.Text = "No subcategory";
                    txtCategory.Text = "No category";
                }


            }
        }

        private void loadDropDown()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var list2 = from cat in dc.ProductCategories
                            select cat;
                ddCategory.Items.Clear();
                foreach (ProductCategory cat in list2)
                    ddCategory.Items.Add(new ListItem(cat.Name, cat.ProductCategoryID.ToString()));
                if (txtCategory.Text != "No category")
                    ddCategory.Items.FindByText(txtCategory.Text).Selected = true;
                //else
                //    ddCategory.Items[0].Selected = true;

                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                ddSubcategory.Items.Clear();
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
                if (txtSubcategory.Text != "No subcategory")
                    ddSubcategory.Items.FindByText(txtSubcategory.Text).Selected = true;
            }



        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            SaveData();
            EditOFF();
        }

        private void EditON()
        {
            loadDropDown();

            ControlCollection kontrolki = Master.FindControl("ContentPlaceHolder1").Controls;
            foreach (Control k in kontrolki)
            {
                if (k is TextBox)
                    ((TextBox)k).Enabled = true;
                if (k is CheckBox)
                    ((CheckBox)k).Enabled = true;
            }
            lnkAnuluj.Visible = true;
            lnkSave.Visible = true;
            lnkEdit.Visible = false;
            txtCategory.Visible = false;
            txtSubcategory.Visible = false;
            ddCategory.Visible = true;
            ddSubcategory.Visible = true;

        }

        private void EditOFF()
        {
            ControlCollection kontrolki = Master.FindControl("ContentPlaceHolder1").Controls;
            foreach (Control k in kontrolki)
            {
                if (k is TextBox)
                    ((TextBox)k).Enabled = false;
                if (k is CheckBox)
                    ((CheckBox)k).Enabled = false;
            }
            lnkAnuluj.Visible = false;
            lnkSave.Visible = false;
            lnkEdit.Visible = true;
            txtCategory.Visible = true;
            txtSubcategory.Visible = true;
            ddCategory.Visible = false;
            ddSubcategory.Visible = false;
        }

        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            EditON();
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            //DeleteData();
            //Server.Transfer("list.aspx");
        }

        private void DeleteData()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var produkt = (from prod in dc.Products
                               where prod.ProductID == id
                               select prod).Single();
                dc.Products.DeleteOnSubmit(produkt);
                dc.SubmitChanges();
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            ModalPopupExtender1.Hide();
            DeleteData();
            Response.Redirect("list.aspx");
        }

        protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
              ddSubcategory.Items.Clear();
                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
            }
        }

        protected void lnkAnuluj_Click(object sender, EventArgs e)
        {
            EditOFF();
        }
    }
}